﻿using Grow.Domain.Object;

namespace Grow.Domain.Repository
{
    /// <summary>
    /// 定义数据上下文接口
    /// </summary>
    public interface IDbContext : IDisposable
    {

        /// <summary>
        /// 获取未跟踪查询对象
        /// </summary>
        IQueryable<TAggregateRoot> GetQueryableAsNoTracking<TAggregateRoot>() where TAggregateRoot : class, IAggregateRoot;

        /// <summary>
        /// 获取查询对象
        /// </summary>
        IQueryable<TAggregateRoot> GetQueryable<TAggregateRoot>() where TAggregateRoot : class, IAggregateRoot;


        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="TAggregateRoot"></typeparam>
        /// <param name="aggregateRoot"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task AddAsync<TAggregateRoot>(TAggregateRoot aggregateRoot, CancellationToken cancellationToken = default) where TAggregateRoot : class, IAggregateRoot;

        /// <summary>
        /// 更新
        /// </summary>
        /// <typeparam name="TAggregateRoot"></typeparam>
        /// <param name="aggregateRoot"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task UpdateAsync<TAggregateRoot>(TAggregateRoot aggregateRoot, CancellationToken cancellationToken = default) where TAggregateRoot : class, IAggregateRoot;


        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="TAggregateRoot"></typeparam>
        /// <param name="aggregateRoot"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task RemoveAsync<TAggregateRoot>(TAggregateRoot aggregateRoot, CancellationToken cancellationToken = default) where TAggregateRoot : class, IAggregateRoot;


        /// <summary>
        /// 异步方式提交数据上下文的所有变更
        /// </summary>
        /// <param name="cancelToken">任务取消标识</param>
        /// <returns>操作影响的行数</returns>
        Task<int> SaveChangesAsync(CancellationToken cancelToken = default);


        Task<TAggregateRoot?> FirstOrDefaultAsync<TAggregateRoot>(IQueryable<TAggregateRoot> aggregateRootQueryable, CancellationToken cancellationToken);


        Task<List<TAggregateRoot>> ToListAsync<TAggregateRoot>(IQueryable<TAggregateRoot> aggregateRootQueryable, CancellationToken cancellationToken);
    }
}
